package com.qijian.maindata.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.uuid.IdUtils;
import com.qijian.maindata.domain.ExamineScheme;
import com.qijian.maindata.domain.ExamineSchemeDetails;
import com.qijian.maindata.domain.query.ExamineSchemeDetailsQuery;
import com.qijian.maindata.domain.query.ExamineSchemeQuery;
import com.qijian.maindata.domain.vo.ExamineSchemeVo;
import com.qijian.maindata.mapper.ExamineSchemeMapper;
import com.qijian.maindata.service.IExamineSchemeDetailsService;
import com.qijian.maindata.service.IExamineSchemeService;
import com.qijian.tool.service.IStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检验方案Service业务层处理
 *
 * @author qijian
 * @date 2021-11-24
 */
@Service
@RequiredArgsConstructor
public class ExamineSchemeServiceImpl extends ServiceImpl<ExamineSchemeMapper, ExamineScheme> implements IExamineSchemeService {

    private final ExamineSchemeMapper examineSchemeMapper;
    private final IExamineSchemeDetailsService examineSchemeDetailsService;
    private final IStorageService storageService;

    @Override
    public Page<ExamineSchemeVo> pageVo(Page<ExamineSchemeVo> page, QueryWrapper<ExamineSchemeQuery> wrapper) {
        return examineSchemeMapper.selectPageVo(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveData(ExamineSchemeVo vo) {
        ExamineScheme examineScheme = JSONUtil.parse(vo).toBean(ExamineScheme.class);
        List<ExamineSchemeDetails> examineSchemeDetails = JSONUtil.parseArray(vo.getDetails()).toList(ExamineSchemeDetails.class);
        if (ObjectUtils.isNull(examineScheme.getSchemeId())) {
            examineScheme.setSchemeId(IdUtils.singletonSnowId());
            if (StringUtils.isBlank(examineScheme.getSchemeCode())) {
                examineScheme.setSchemeCode("FA-" + String.format("%06d", examineSchemeMapper.countWithDel(true)));
            }
        } else {
            ExamineSchemeDetailsQuery detailsQuery = new ExamineSchemeDetailsQuery();
            detailsQuery.setSchemeId(examineScheme.getSchemeId());
            examineSchemeDetailsService.remove(WhereEntityTool.invoke(detailsQuery));
        }

        if (CollectionUtil.isNotEmpty(vo.getFiles())) {
            vo.getFiles().forEach(action -> {
                action.setClassTable(ExamineScheme.class.getSimpleName());
                action.setClassId(examineScheme.getSchemeId());
            });
            storageService.updateBatchById(vo.getFiles());
        }

        examineSchemeDetails.forEach(details -> details.setSchemeId(examineScheme.getSchemeId()));

        examineSchemeDetailsService.saveOrUpdateBatch(examineSchemeDetails);
        return ObjectUtils.isNull(vo.getSchemeId()) ? save(examineScheme) : updateById(examineScheme);
    }
}
